package api

import (
	"context"
	"gitee.com/leo-xin/novel_distribution_sdk_go/wending/h5/config"
	"net/http"
	"net/url"
)

type LinkQueryApiService service

type LinkQueryRequest struct {
	ctx        context.Context
	ApiService *LinkQueryApiService
	page       *uint64
	pageSize   *uint64
	siteId     *uint
	sourceUuId *string
	startTime  *uint64
	endTime    *uint64
	shortLink  *string
	linkName   *string
	linkGroup  *int
}

type LinkQueryResponse struct {
	Code *int64         `json:"code,omitempty"`
	Data *LinkQueryData `json:"data,omitempty"`
}

type LinkQueryData struct {
	TotalPage *uint64         `json:"totalPage,omitempty"`
	List      []*LinkListItem `json:"list,omitempty"`
}

type LinkListItem struct {
	LinkId         *uint64 `json:"linkId,omitempty"`
	LinkName       *string `json:"linkName,omitempty"`
	ShortLink      *string `json:"shortLink,omitempty"`
	CreateTime     *uint64 `json:"createTime,omitempty"`
	EntryArticle   *string `json:"entryArticle,omitempty"`
	LinkClickCount *uint64 `json:"linkClickCount,omitempty"`
	RechargeCount  *uint64 `json:"rechargeCount,omitempty"`
	RechargeAmount *uint64 `json:"rechargeAmount,omitempty"`

	SourceUuid *string `json:"sourceUuid,omitempty"`
	BookTitle  *string `json:"bookTitle,omitempty"`
}

func (r *LinkQueryRequest) Page(page uint64) *LinkQueryRequest {
	r.page = &page
	return r
}

func (r *LinkQueryRequest) PageSize(pageSize uint64) *LinkQueryRequest {
	r.pageSize = &pageSize
	return r
}

func (r *LinkQueryRequest) SiteId(siteId uint) *LinkQueryRequest {
	r.siteId = &siteId
	return r
}

func (r *LinkQueryRequest) SourceUuId(sourceUuId string) *LinkQueryRequest {
	r.sourceUuId = &sourceUuId
	return r
}

func (r *LinkQueryRequest) StartTime(startTime uint64) *LinkQueryRequest {
	r.startTime = &startTime
	return r
}

func (r *LinkQueryRequest) EndTime(endTime uint64) *LinkQueryRequest {
	r.endTime = &endTime
	return r
}

func (r *LinkQueryRequest) ShortLink(shortLink string) *LinkQueryRequest {
	r.shortLink = &shortLink
	return r
}

func (r *LinkQueryRequest) LinkName(linkName string) *LinkQueryRequest {
	r.linkName = &linkName
	return r
}

func (r *LinkQueryRequest) LinkGroup(linkGroup int) *LinkQueryRequest {
	r.linkGroup = &linkGroup
	return r
}

func (r *LinkQueryRequest) Execute() (*LinkQueryResponse, *http.Response, error) {
	return r.ApiService.getExecute(r)
}

func (r *LinkQueryRequest) ConsumerKey(ConsumerKey string) *LinkQueryRequest {
	r.ctx = context.WithValue(r.ctx, config.ConsumerKey, ConsumerKey)
	return r
}

func (r *LinkQueryRequest) SecretKey(SecretKey string) *LinkQueryRequest {
	r.ctx = context.WithValue(r.ctx, config.SecretKey, SecretKey)
	return r
}

func (r *LinkQueryRequest) WithLog(enable bool) *LinkQueryRequest {
	if enable {
		r.ctx = context.WithValue(r.ctx, config.ContextEnableLog, true)
	}
	return r
}

func (a *LinkQueryApiService) Get(ctx context.Context) *LinkQueryRequest {
	return &LinkQueryRequest{
		ApiService: a,
		ctx:        ctx,
	}
}

func (a *LinkQueryApiService) getExecute(r *LinkQueryRequest) (*LinkQueryResponse, *http.Response, error) {

	cfg := a.client.Cfg
	var (
		localVarPath         = cfg.GetBasePath() + "/dist-api/link/query"
		localVarHTTPMethod   = http.MethodGet
		localVarPostBody     map[string]string
		localVarFormParams   = url.Values{}
		localVarHeaderParams = make(map[string]string)
		localVarQueryParams  = url.Values{}
		localVarReturnValue  *LinkQueryResponse
	)

	r.ctx = a.client.prepareCtx(r.ctx)

	if ok, GenericOpenAPIError := hasReqParam(r.page, "Page"); !ok {
		return localVarReturnValue, nil, GenericOpenAPIError
	}

	if ok, GenericOpenAPIError := hasReqParam(r.siteId, "SiteId"); !ok {
		return localVarReturnValue, nil, GenericOpenAPIError
	}

	localVarQueryParams.Add("page", toString(r.page))
	localVarQueryParams.Add("siteid", toString(r.siteId))

	if r.pageSize != nil {
		localVarQueryParams.Add("pageSize", toString(r.pageSize))
	}
	if r.sourceUuId != nil {
		localVarQueryParams.Add("sourceuuid", toString(r.sourceUuId))
	}
	if r.startTime != nil {
		localVarQueryParams.Add("starttime", toString(r.startTime))
	}
	if r.endTime != nil {
		localVarQueryParams.Add("endtime", toString(r.endTime))
	}
	if r.shortLink != nil {
		localVarQueryParams.Add("shortlink", toString(r.shortLink))
	}
	if r.linkName != nil {
		localVarQueryParams.Add("linkname", toString(r.linkName))
	}
	if r.linkGroup != nil {
		localVarQueryParams.Add("linkgroup", toString(r.linkGroup))
	}

	req, err := a.client.buildRequest(r.ctx, localVarPath, localVarHTTPMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams)
	if err != nil {
		return localVarReturnValue, nil, err
	}

	localVarHTTPResponse, err := a.client.call(r.ctx, req, &localVarReturnValue)
	if err != nil || localVarHTTPResponse == nil {
		return localVarReturnValue, localVarHTTPResponse, err
	}
	return localVarReturnValue, localVarHTTPResponse, nil
}
